<script setup lang="ts">
  import { ref, reactive, watch, computed } from 'vue';
  import { TableRowSelection, Notification } from '@arco-design/web-vue';
  import { useBooleanStore } from '@/store';
  import { getUserJob } from '@/utils/auth';
  import { searchProblemType, insertDto } from '@/api/material-receipt';

  const booleanStore = useBooleanStore();
  const hStyle = ref({
    'background-color': booleanStore.color,
    'border-radius': booleanStore.borderRadius,
  });
  const showDrawer = ref<boolean>(false);

  const columns: any[] = [
    {
      title: '序号',
      dataIndex: 'PAGEHELPER_ROW_ID',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '需求单位',
      dataIndex: 'requireShop',
    },
    {
      title: '条码',
      dataIndex: 'barcode',
    },
    {
      title: '交接单号',
      dataIndex: 'deliveryPlanCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '合格证信息',
      dataIndex: 'mesCheckQualiticationId',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '物料名称',
      dataIndex: 'itemName',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '机型',
      dataIndex: 'productCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '图号',
      dataIndex: 'itemCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },

    {
      title: '型号',
      dataIndex: 'mProductNumber',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '质量编号',
      dataIndex: 'qualityCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },

    {
      title: '批次',
      dataIndex: 'batchNo',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '架次',
      dataIndex: 'startLotNo',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },

    {
      title: '数量',
      dataIndex: 'deliveryQty',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '计量单位',
      dataIndex: 'mdsUnit',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '物料大类',
      dataIndex: 'itemType',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },

    {
      title: '制造单位',
      dataIndex: 'attribute18',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '移交人工号',
      dataIndex: 'empName',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '移交时间',
      dataIndex: 'deliveryDate',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
  ];
  const localTableData = ref<any[]>([]);
  const rowSelection: TableRowSelection = reactive({
    type: 'checkbox',
    showCheckedAll: true,
    onlyCurrent: false,
  });
  const selectedKeys = ref<any>([]);

  const productCode = ref<string>('');
  const batchNo = ref<string>('');
  const materialNum = ref<string>('');

  // 数据库一共有多少条
  const total = ref<number>(0);
  const pageNumber = ref<number>(1);
  const pageSize = ref<number>(10);

  // 问题类型
  const problemType = ref<string>('');
  const problemTypeList = ref<any[]>([]);
  // 问题内容
  const problemContent = ref<string>('');

  // const flowQueryData = ref({
  //   searchParams: computed(() => ({
  //     productCode: productCode.value,
  //     batchNo: batchNo.value,
  //     materialNum: materialNum.value,
  //   })),
  //   pageParameter: computed(() => ({
  //     rows: pageSize.value,
  //     page: pageNumber.value,
  //   })),
  // });

  async function search() {
    const data: any = [];
    localTableData.value = data?.responseBody?.result || [];
    total.value = data?.responseBody?.pageParameter?.totalCount || 0;
  }
  async function getProblemType() {
    const data = await searchProblemType('');
    problemTypeList.value = data.responseBody;
  }
  function reset() {
    productCode.value = '';
    batchNo.value = '';
    materialNum.value = '';
    search();
  }
  // 监听弹框
  watch(
    () => booleanStore.boolValue,
    (newValue: boolean) => {
      showDrawer.value = newValue;
      search();
      getProblemType();
      productCode.value = '';
      batchNo.value = '';
      materialNum.value = '';
      localTableData.value = [];
      selectedKeys.value = [];
    }
  );

  function afterVisibleChange(val: any) {
    if (!val) {
      booleanStore.toggleBool(0.1);
    }
  }
  function handleCancel() {
    booleanStore.toggleBool(0.1);
  }
  const getUserJobNumber = ref<any>(getUserJob());
  async function handleOk() {
    if (selectedKeys.value.length === 0) {
      Notification.warning({
        title: '提示',
        content: '请选择交接单',
        closable: true,
      });
      return;
    }
    if (problemType.value === '') {
      Notification.warning({
        title: '提示',
        content: '请选择问题类型',
        closable: true,
      });
      return;
    }
    if (problemContent.value === '') {
      Notification.warning({
        title: '提示',
        content: '请填写问题内容',
        closable: true,
      });
      return;
    }
    const selectedData = localTableData.value.filter((item: any) =>
      selectedKeys.value.includes(item.PAGEHELPER_ROW_ID)
    );
    const info = {
      wmsDeliveryPlans: selectedData,
      problemType: problemType.value,
      problemContent: problemContent.value,
      applyEmpcode: getUserJobNumber.value.value5,
    };
    const data: any = await insertDto(info);
    if (data.retCode === '200') {
      Notification.success({
        title: '成功',
        content: data.message,
        closable: true,
      });
      booleanStore.toggleBool(0.1);
    } else {
      Notification.error({
        title: '失败',
        content: data.message,
        closable: true,
      });
      booleanStore.toggleBool(0.1);
    }
    booleanStore.watchUpdate(8);
  }
  function change(e: any) {
    pageNumber.value = e;
  }
  function changeSize(e: any) {
    pageSize.value = e;
  }
  watch([pageNumber, pageSize], () => {
    search();
  });
</script>

<template>
  <a-drawer
    width="70%"
    :height="340"
    :visible="showDrawer"
    placement="right"
    @after-visible-change="afterVisibleChange"
    @cancel="handleCancel"
    @ok="handleOk"
  >
    <template #title> 新增AO预配料信息</template>
    <a-card :header-style="hStyle" :bordered="false">
      <template #title>
        <div class="font">查询条件</div>
      </template>
      <!-- 第一行 -->
      <a-row :gutter="{ md: 8, lg: 24, xl: 32 }">
        <a-col :span="6">
          <div>
            <a-input v-model="productCode" placeholder="机型" size="large" />
          </div>
        </a-col>
        <a-col :span="6">
          <div>
            <a-input v-model="batchNo" placeholder="批次" size="large" />
          </div>
        </a-col>
        <a-col :span="6">
          <div>
            <a-input
              v-model="materialNum"
              placeholder="物料名称"
              size="large"
            />
          </div>
        </a-col>
        <a-col :span="6">
          <a-button type="primary" size="large" @click="search">查询</a-button>
          <a-button class="btn-reset" size="large" @click="reset"
            >重置</a-button
          >
        </a-col>
      </a-row>
    </a-card>
    <a-card :header-style="hStyle" :bordered="false">
      <template #title>
        <div class="font">交接单数据</div>
      </template>
      <a-table
        v-model:selectedKeys="selectedKeys"
        :stripe="true"
        class="top-table"
        :pagination="false"
        row-key="PAGEHELPER_ROW_ID"
        :columns="columns"
        :data="localTableData"
        :row-selection="rowSelection"
      >
      </a-table>
      <div class="page-position">
        <a-pagination
          :total="total"
          :current="pageNumber"
          :page-size="pageSize"
          show-total
          show-jumper
          show-page-size
          @change="change"
          @update:page-size="changeSize"
        />
      </div>
    </a-card>
  </a-drawer>
</template>

<style scoped>
  .top-table {
    white-space: nowrap;
  }
  .row-top {
    margin-top: 10px;
  }
  .btn-reset {
    margin-left: 10px;
  }
  .font {
    font-size: 20px;
    color: black;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
      Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue',
      sans-serif;
  }
  .page-position {
    margin-top: 20px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
